package com.supor.aiot.mine.ui.feedback;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Message;

import com.android.baseconfig.base.BaseHandler;
import com.android.baseconfig.common.utils.ThreadUtils;
import com.android.baseconfig.common.utils.ToastUtils;
import com.supor.aiot.common.app.AppAgent;
import com.supor.aiot.common.app.Constants;
import com.supor.aiot.common.base.BaseConfigActivity;
import com.supor.aiot.common.base.NoViewModel;
import com.supor.aiot.mine.R;
import com.supor.aiot.mine.bean.ImageItem;
import com.supor.aiot.mine.databinding.ActivityImageGridBinding;
import com.supor.aiot.mine.helper.AlbumHelper;
import com.supor.aiot.mine.helper.FeedbackImageCache;
import com.supor.aiot.mine.ui.adapter.ImageGridAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * ---------------------------------------------------------------- <br>
 * Copyright (C) 2014-2020, by het, Shenzhen, All rights reserved.  <br>
 * ---------------------------------------------------------------- <br>
 * <p>
 * 描述: 手机图片列表 <br>
 * 作者: lei <br>
 * 日期: 2021/2/1 <br>
 */
public class ImageGridActivity extends BaseConfigActivity<NoViewModel, ActivityImageGridBinding> implements BaseHandler.BaseHandlerCallBack {
    private static final int QUERY_OK = 1;
    private ImageGridAdapter adapter;
    private List<ImageItem> dataList;
    private BaseHandler<BaseHandler.BaseHandlerCallBack> mHandler;
    /**
     * 最多选择多少图片
     */
    private static int MAX_SELECT = 8;
    // type 0-photo|1-video
    private int type = 0;

    @Override
    protected int resourceId() {
        return R.layout.activity_image_grid;
    }

    @Override
    protected boolean isUseBlackFontWithStatusBar() {
        return true;
    }

    @Override
    public void callBack(final Message message) {
        if (message.what == 0) {
            ToastUtils.getInstance().showCustomToast("最多选择" + MAX_SELECT + "个");
        } else if (QUERY_OK == message.what) {
            adapter.notifyData(dataList);
        }
    }

    public void initData() {

        ThreadUtils.execute(() -> {
            if (type == 0) {
                dataList = AlbumHelper.getImageList(AppAgent.getApp());
            } else {
                dataList = AlbumHelper.getVideoList(AppAgent.getApp());
            }
            // dataList =
            //         AlbumHelper.getImageListBybucketId(AppAgent.getApp(),
            //                 ImageGridActivity
            //         .bucketId);
            mHandler.sendEmptyMessage(QUERY_OK);
        });
        bindingView.btnFinish.setOnClickListener(view -> {
            showLoading(getString(R.string.loading), false);
            final ArrayList<ImageItem> list = new ArrayList<>(adapter.getMap().values());
            for (int i = 0; i < list.size(); ++i) {
                if (FeedbackImageCache.getInstance().getDrr().size() < MAX_SELECT) {
                    FeedbackImageCache.getInstance().getDrr().add(list.get(i).imagePath);
                    FeedbackImageCache.getInstance().getSelectItem().add(list.get(i));
                }
            }
            closeLoading();
            setResult(type);
            finish();
        });
    }

    public void initView() {
        mHandler = new BaseHandler<>(this);
        Intent intent = getIntent();
        if (intent != null) {
            MAX_SELECT = intent.getIntExtra(Constants.Key.NUM, 8);
            type = intent.getIntExtra(Constants.Key.param_type, 0);
        }
        if (type == 0) {
            bindingView.toolbar.setTitle(R.string.pick_pic);
        } else {
            bindingView.toolbar.setTitle(R.string.pick_video);
        }
        bindingView.toolbar.setTvLeftOnClickListener(v -> finish());
        bindingView.toolbar.setIvLeftOnClickListener(v -> finish());
        bindingView.gridview.setSelector(new ColorDrawable(0));
        adapter = new ImageGridAdapter(this, dataList, mHandler);
        adapter.setMaxNum(MAX_SELECT);
        bindingView.gridview.setAdapter(adapter);
        adapter.setTextCallback(n -> bindingView.btnFinish.setText((getString(R.string.sure) + "(" + n + ")")));
        bindingView.gridview.setOnItemClickListener((adapterView, view, n, n2) -> adapter.notifyDataSetChanged());
        bindingView.toolbar.setIvLeftOnClickListener(v -> finish());
    }
}
